package VisaoControleAcademico;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import ControleAcamico.ContrlMatricula;
import ControleAcamico.ControlePrincipal;
import ControleDiversas.AtualizarListaAlunos;
import ModeloAcademico.ModeloMatricula;

public class PainelListaAluno extends JFrame {
	private JPanel painelP,painelB;
	public static JTable tabelaAluno;
	private JScrollPane jscrollListaAluno;
	private JButton butaoConf,butaoCan;
	private ControlePrincipal controle;
	static int d = 0;
	public static DefaultTableModel  dtm;
	
	public PainelListaAluno(ControlePrincipal controle) {
		this.controle = controle;
			init();
			Layout();
			componetes();
			eventos();
			executar();		
	}

	private void eventos() {
		
			butaoCan.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
						hide();
				}
			});
			
			butaoConf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		DefaultTableModel tableModelo = (DefaultTableModel)MatriculaView.tabelaMatri.getModel();
		tableModelo.setNumRows(d);
		d++;
			int linha = tabelaAluno.getSelectedRow();			
			String l1 = tabelaAluno.getValueAt(linha, 0).toString();
			String l5 = tabelaAluno.getValueAt(linha, 4).toString();
			
			if (d > 0) {
				for (int i = 0; i < MatriculaView.tabelaMatri.getRowCount(); i++) {
					String altenome1 = MatriculaView.tabelaMatri.getValueAt(i, 0).toString();
					if (l1.equals(altenome1)) {
						JOptionPane.showMessageDialog(null, "Nome JA Existe!!!");
					}
				}				
			}

						Vector<String>lista = new Vector<String>();
						lista.add(l1);
						lista.add(l5);
						dtm = (DefaultTableModel)MatriculaView.tabelaMatri.getModel();
						dtm.addRow(lista);
					
				}
			});
			
		
	}

	private void executar() {
		setUndecorated( true );
		setSize(450, 350);
		centralizarComponente();
	}
	public void centralizarComponente() {  
        Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();  
        Dimension dw = getSize();  
        setLocation((ds.width - dw.width) / 3, (ds.height - dw.height) / 2);  
    } 
	private void Layout() {
		painelP.setLayout(new FlowLayout(FlowLayout.CENTER));
	}

	private void componetes() {
		painelP.add(painelB);
		painelB.add(jscrollListaAluno);
		painelB.add(butaoConf);
		painelB.add(butaoCan);
		add(painelP);
		
	}

	private void init() {
		List();
		painelP = new JPanel();
		painelP.setPreferredSize(new Dimension(500,600));
		painelB = new JPanel();
		painelB.setPreferredSize(new Dimension(500,600));
		
		butaoConf = new JButton("Adiconar A Lista Alunos");
		butaoCan = new JButton("Sair");
	}
	private void List() {
		tabelaAluno = new JTable();
		jscrollListaAluno = new JScrollPane();
		tabelaAluno.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
				"Nome", "Enderedo", "Email", "Data Nacimento","cpf","rg"}));
		jscrollListaAluno.setPreferredSize(new Dimension(440, 300));
		jscrollListaAluno.setViewportView(tabelaAluno);	
		
	}
	public void mostrarListaAlunos() {
		setVisible(true);
		try {
			AtualizarListaAlunos.pesquisar();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


}
